package com.laundry.service.impl;

import com.laundry.dao.PromotionDao;
import com.laundry.entity.Promotion;
import com.laundry.service.PromotionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PromotionServiceImpl implements PromotionService {

    @Autowired
    private PromotionDao promotionDao;
    
    @Override
    public List<Promotion> findAll() {
        return promotionDao.findAll();
    }
    
    @Override
    public List<Promotion> findActivePromotions() {
        return promotionDao.findByStatus(1); // 1表示活动状态
    }
    
    @Override
    public Promotion findById(Long id) {
        return promotionDao.findById(id).orElse(null);
    }
    
    @Override
    public Promotion save(Promotion promotion) {
        promotion.setCreateTime(new Date());
        promotion.setUpdateTime(new Date());
        return promotionDao.save(promotion);
    }
    
    @Override
    public Promotion update(Promotion promotion) {
        Promotion existPromotion = promotionDao.findById(promotion.getId()).orElse(null);
        if (existPromotion == null) {
            return null;
        }
        
        promotion.setCreateTime(existPromotion.getCreateTime());
        promotion.setUpdateTime(new Date());
        return promotionDao.save(promotion);
    }
    
    @Override
    public void deleteById(Long id) {
        promotionDao.deleteById(id);
    }
} 